﻿using Net.Sz;
using Net.Sz.Framework.Netty;
using Net.Sz.Framework.Netty.Buffer;
using Net.Sz.Framework.Netty.Tcp;
using Net.Sz.Framework.Szlog;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SocketClient
{

    class Program
    {

        private static SzLogger log = SzLogger.getLogger();

        static void Main(string[] args)
        {
            /*创建客户端连接的处理handler程序*/
            NettyPool.ClientSessionHandler = new ClientHandler();
            /*客户端创建和服务器的连接*/
            NettyClient clientA = new NettyClient(ip: "www.baidu.com", port: 80);
            /*连接服务器*/
            clientA.Connect();
            {
                String msgString = "";
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 102; j++)
                    {
                        msgString += "s";
                    }
                    msgString += "\n";
                }

                /*封装书写消息*/
                BufferWriter bw = new BufferWriter();
                bw.Write((int)1);
                bw.Write((long)0);
                byte[] msgBytes = System.Text.UTF8Encoding.UTF8.GetBytes(msgString);
                ///*发送一个 string 类型数据*/
                //bw.Write("我是需要发送的字符串");

                //Bitmap bitmap = new Bitmap("filename");
                ///*发送图片*/
                //bitmap.Save(bw.BaseStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                ///*获取需要发送消息的 buff */
                //byte[] msg = bw.GetBuffer();
                ///*发送消息到服务器*/
                //clientA.WriteAndFlush(2, msg);

            }
            Console.ReadLine();
        }

        /// <summary>
        /// 客户端连接的处理handler程序
        /// </summary>
        class ClientHandler : IIOSessionHandler
        {

            public void ChannelActive(IOSession session)
            {

            }

            public void ExceptionCaught(IOSession session, Exception ex)
            {

            }

            public void ChannelRead(IOSession session, int msgId, byte[] buffer)
            {
                /*收到服务器发送来的消息*/

                if (msgId == 2)
                {

                    /*封装服务器发来的消息，读取内容*/
                    BufferReader br = new BufferReader(buffer);
                    /*读取服务器发送的long 类型数据*/
                    long mmid = br.ReadInt64();

                    /*收到消息回复消息处理*/
                    log.Error("收到服务器消息：" + session.ID + "  消息唯一回复id" + mmid);

                }
            }

            public void ChannelUnregistered(IOSession session, params string[] obj)
            {

            }

            public void ChannelInactive(IOSession session)
            {
            }
        }
    }
}
